package com.kensure.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import co.kensure.frame.ResultInfo;
import co.kensure.frame.ResultRowInfo;
import co.kensure.frame.ResultRowsInfo;
import co.kensure.http.RequestUtils;
import co.kensure.mem.PageInfo;

import com.alibaba.fastjson.JSONObject;
import com.kensure.shike.cms.model.SKCms;
import com.kensure.shike.cms.service.SKCmsService;

/**
 * 消息列表
 * 
 * @author fankaidi
 *
 */
@Controller
@RequestMapping(value = "cms")
public class SKCmsController {

	@Resource
	private SKCmsService sKCmsService;

	/**
	 * 信息列表
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "cmslist.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo cmslist(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		int typeid = json.getInteger("typeid");
		List<SKCms> list = sKCmsService.selectByTypeId(typeid);
		return new ResultRowsInfo(list);
	}

	/**
	 * 后台信息列表
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "alllist.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo alllist(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		PageInfo page = JSONObject.parseObject(json.toJSONString(), PageInfo.class);
		List<SKCms> list = sKCmsService.selectByWhere(page);
		Long count = sKCmsService.selectCountByWhere(null);
		return new ResultRowsInfo(list, count);
	}

	/**
	 * 信息内容
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "get.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo get(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long id = json.getLong("id");
		SKCms cms = sKCmsService.detail(id);
		return new ResultRowInfo(cms);
	}
	
	/**
	 * 信息内容删除
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "del.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo del(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long id = json.getLong("id");
		sKCmsService.delete(id);
		return new ResultRowInfo();
	}
	/**
	 * 保存消息个体
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "save.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo save(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		SKCms obj = JSONObject.parseObject(json.toJSONString(), SKCms.class);	
		sKCmsService.add(obj);
		return new ResultRowInfo();
	}
}
